<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/base.js"></script>
</head>
<body>
<h1>课程管理</h1>
1、<button onclick="goAddPage();">新增课程</button><br><br>
3、<input id="likeName" placeholder="请输入课程名"/>&nbsp;&nbsp;&nbsp;&nbsp;<button onclick="onSearchClick();">查找课程</button><br>
<br>
<div>
<table id="courseData" border="1px">


</table>
<br>
<div  id="buttons" align="left" style="position: relative;left:287px">
</div>
</div>
</body>
<script>

    let pageNo=1;
    let pageSize=5;
    let pageCount=0;

    function goAddPage() {
        window.location.href="addCourse.html";
    }
    function onSearchClick() {
        pageNo=1;
        pageSize=5;
        let name=document.getElementById("likeName").value;
        loadData(pageNo,pageSize,name);
    }

    function prevPage() {
        pageNo--;
        if (pageNo<=0){
            pageNo=1;
        }
        let name=document.getElementById("likeName").value;
        loadData(pageNo,pageSize,name);
    }
    function nextPage() {
        pageNo++;
        if (pageNo>pageCount){
            pageNo=pageCount;
        }
        let name=document.getElementById("likeName").value;
        loadData(pageNo,pageSize,name);
    }
    function goPage(pNo) {
        pageNo=pNo;
        let name=document.getElementById("likeName").value;
        loadData(pageNo,pageSize,name);
    }

    function loadData(pageNo,pageSize,name){
        let url="http://localhost:9001/Demo/course/page";
        let method="post";
        let json={};
        json.pageNo=pageNo;
        json.pageSize=pageSize;
        json.likeName=name;
        $.rest(method,url,json,function (r) {
            if (r.code=200){

                //装配数据
                let innerHTML=" <tr>\n" +
                    "        <td>课程号</td>\n" +
                    "        <td>课程名称</td>\n" +
                    "        <td>授课教师</td>\n" +
                    "        <td>教室容量</td>\n" +
                    "        <td>当前选课人数</td>\n" +
                    "        <td colspan=\"3\" align=\"center\">操作</td>\n" +
                    "    </tr>";
                let courses = r.result.courses;
                for (let i = 0; i <courses.length ; i++) {
                    let course=courses[i];
                    let courseHtml=" <tr>\n" +
                        "        <td>"+course.id+"</td>\n" +
                        "        <td>"+course.name+"</td>\n" +
                        "        <td>"+course.username+"</td>\n" +
                        "        <td>"+course.sMax+"</td>\n" +
                        "        <td>"+course.sSum+"</td>\n" +
                        "        <td><a href=\"#\" onclick=\"return onUpdateClick("+course.id+",'"+course.name+"','"+course.username+"',"+course.sMax+");\">修改</a></td>\n" +
                        "        <td><a href=\"#\" onclick=\"return onDeleteClick("+course.id+");\">删除</a></td>\n" +
                        "        <td><a href=\"#\" onclick='goToManageCourse("+course.id+","+course.sSum+","+course.sMax+")'>管理当前选课</a></td>\n" +
                        "    </tr>";
                    innerHTML+=courseHtml;
                }
                document.getElementById("courseData").innerHTML=innerHTML;

                pageCount=r.result.pageCount;
                innerHTML="<button onclick='prevPage();'>上一页</button>";
                for (let i = 0; i <pageCount ; i++) {
                    innerHTML+="<button onclick='goPage("+(i+1)+");'>"+(i+1)+"</button>";
                }
                innerHTML+="<button onclick='nextPage();'>下一页</button>";
                document.getElementById("buttons").innerHTML=innerHTML;
            }
        });
    }

    //当页面加载完成就调用的方法
    window.onload=function () {
        pageNo=1;
        pageSize=5;
        loadData(pageNo,pageSize,"");
    };
    function goToManageCourse(cid,sSum,sMax) {
        localStorage.setItem("cid",cid);
        localStorage.setItem("sSum",sSum);
        localStorage.setItem("sMax",sMax);
        window.location.href="manageCourse.html";
    }
    /**
     * 修改课程的方法
    */
    function onUpdateClick(cid,name,teacherName,sMax) {
        sessionStorage.setItem("cid",cid);
        sessionStorage.setItem("name",name);
        sessionStorage.setItem("teacherName",teacherName);
        sessionStorage.setItem("sMax",sMax);
        window.location.href="updateCourse.html";

    }

     /**
     * 删除课程的方法
     */
    function onDeleteClick(cid) {
        let url="http://localhost:9001/Demo/course/course";
        let method="delete";
        let json={};
        json.cid=cid;
        $.rest(method,url,json,function (r) {
            if (r.code==200){
                pageSize=5;
                let name=document.getElementById("likeName").value;
                loadData(pageNo,pageSize,name);
            }
        });
        return false;
    }


</script>
</html>